The same as approach, for whole numbers: moves start toward end by step and never overshoots - landing exactly on end once step would carry it past - and the answer is a whole number. Use this one for something counted that should creep toward a target a little each frame, such as a score display catching up with the real score.
approach_int(start, end, step) -> int| Name | Type |
|---|---|
start | int |
end | int |
step | int |
int
The same as approach, for whole numbers: moves start toward end by step and never overshoots - once step would carry it past end, this lands exactly on end instead - and the answer is a whole number. step is always a plain distance, never signed; which way to move is worked out from start and end themselves.
Reach for this one whenever the number is counted rather than measured and should creep toward a target a little at a time: a score display that catches up with the real score, a coin counter that ticks up, a timer that winds down to zero. Use approach for things that are measured - a position, a speed, a volume.
var shown = 0;
shown = approach_int(shown, 1000, 30);Moves shown 30 closer to 1000, so it becomes 30. A real score display keeps shown as a value of its own and calls this once every frame, so it counts up 30 at a time and stops on 1000 exactly - never 1010. shown is a whole number, so it can be handed to anything that wants one.
approach and approach_int do the same arithmetic. They differ in the type of the answer, and that difference is worth a function of its own because of what goes wrong without it.
approach hands back a real. So this line:
display = approach(display, 100, 3); <- the trap: this is NOT what you want hereturns display into a real - the value is still a whole number, but the kind of number has changed. Nothing complains there. The complaint arrives later, somewhere else, wherever display is next handed to something that wants a whole number, such as the frame of draw_sprite, which says which picture of a sprite to show:
Argument 4 to `draw_sprite` should be an int, but this is a real.That error points at a line that looks perfectly fine, and it may be in a different file from the assignment that caused it. Written with approach_int, it cannot happen.
approach_int takes whole numbers only. A number with a point in it is a real - even 4.0 - and is refused when the project is checked:
Argument 3 to `approach_int` should be an int, but this is a real.That is what you get from approach_int(display, 100, 0.5): a step of half a unit is not a whole number. If you have a real and want a whole number out of it, round it first with int_floor, int_ceil or int_round. If the numbers really are measured, use approach instead.
0, or already thereA step of 0 makes no progress at all - approach_int just returns start unchanged. If start already equals end, the result is end whatever step is. A negative step is not what it is for - it moves away from end, exactly as it does in approach.
A real runs out at about 524,000 and a whole number does not run out until about 2.1 billion, so approach_int(0, 1000000, 300000) is 300000, where 1000000 is already more than a real can hold. The check "is the step big enough to arrive?" cannot be fooled by a huge step or two numbers a long way apart either.
One comparison on how far there is to go - cheap enough for every frame, on as many values as a project needs eased toward a target at once.
approach and approach_int differ in TYPE, not in arithmetic. approach hands back a real, so `shown = approach(shown, score, 3)` turns a whole number into a real one, and the next place that wants a whole number refuses it - usually somewhere else entirely, which makes it a puzzle. approach_int hands back a whole number, so it cannot happen. step is a MAGNITUDE, never signed: which way to move is worked out from start and end, a step of 0 makes no progress, and (as in approach) a negative step would move away from end. All three numbers have to be whole: approach_int(n, 100, 0.5) is refused when the project is checked, and so is a variable that holds a real, even one that happens to hold 4.0 - round it first with int_floor, int_ceil or int_round. A whole number goes much further than a real: approach_int(0, 1000000, 300000) is 300000, and 1000000 is already past what a real can hold. One comparison on how far there is to go, cheap enough for every frame.